package it.win4buy.services
{
    import flash.events.IEventDispatcher;

    import it.win4buy.events.RegistrationPlaceHolderEvent;
    import it.win4buy.mock.RemoteObjectMock;
    import it.win4buy.utility.ApplicationConstants;
    import it.win4buy.utility.LoggerUtilty;

    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;

    /**
     *
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class RegistrationPlaceHolderService
    {



        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------

        [Dispatcher]
        public var dispatcher:IEventDispatcher;


        [Inject]
        public var log:LoggerUtilty;


        [Inject]
        public var _ro:RemoteObjectMock;

        private var ro:RemoteObject;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------



        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function RegistrationPlaceHolderService()
        {
        }

        //-------------------------------------------
        //
        // Mediator Handlers
        //
        //-------------------------------------------

        //-------------------------------------------
        //
        // Public Methods
        //
        //-------------------------------------------

        public function execute(userId:String, placeHolderId:String):void
        {
            ro=new RemoteObject();
            log.logToConsole(this, "Registering userId[ " + userId + " ] in the PLaceHolderId [" + placeHolderId + " ]....");

            ro.destination=ApplicationConstants.DESTINATION_TEST
            ro.addEventListener(ResultEvent.RESULT, serviceResult);
            ro.addEventListener(FaultEvent.FAULT, serviceFault);

            ro.tryRegistrationPlaceHolder(userId, placeHolderId);

        }

        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------

        private function serviceResult(event:ResultEvent):void
        {
            log.logToConsole(this, "UserId registered.");

            dispatcher.dispatchEvent(new RegistrationPlaceHolderEvent(RegistrationPlaceHolderEvent.RESULT, event.result, true));
            ro.removeEventListener(ResultEvent.RESULT, serviceResult);
            ro.removeEventListener(FaultEvent.FAULT, serviceFault);
            ro=null;

        }

        private function serviceFault(event:FaultEvent):void
        {

            dispatcher.dispatchEvent(new RegistrationPlaceHolderEvent(RegistrationPlaceHolderEvent.FAULT, event.message, true));
            ro.removeEventListener(ResultEvent.RESULT, serviceResult);
            ro.removeEventListener(FaultEvent.FAULT, serviceFault);
            ro=null;
        }


    }
}